package com.lovo.dao.impl;

import com.lovo.bean.User;
import com.lovo.dao.BaseDAO;
import com.lovo.dao.UserDAO;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Repository
public class UserDAOImpl extends BaseDAO implements UserDAO {
    @Override
    public User getByUsernameAndPwd(String username, String pwd) {
        Connection con = this.getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        User user = null;
        String sql = "select * from t_user where u_username=? and u_pwd=?";

        try {
            statement = con.prepareStatement(sql);
            statement.setString(1,username);
            statement.setString(2,pwd);
            rs = statement.executeQuery();
            while(rs.next()){
                user = new User();
                user.setId(rs.getInt("u_id"));
                user.setUsername(rs.getString("u_username"));
                user.setPwd(rs.getString("u_pwd"));
            }
        } catch (SQLException e) {
        }finally {
            this.close(con,statement,rs);
        }
        return user;
    }

    public static void main(String[] args) {
        UserDAO userDAO = new UserDAOImpl();
        System.out.println(userDAO.getByUsernameAndPwd("admin","1234586"));
    }
}
